﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using LM.YF.K3Cloud.Plugin.Models;
using System;
using System.ComponentModel;
using System.Linq;

/// <summary>
/// 运输计价表表单插件
/// </summary>
/// <remarks>
/// 功能：1、
/// 创建人：lxl
/// 创建日期：2023-12-27
/// 修改信息（1）：修改人，修改日期，修改内容
/// </remarks>
namespace LM.YF.K3Cloud.Plugin
{
    [HotUpdate, Description("运输计价表表单插件")]
    public class ShipRutePriceBillPlugin : AbstractBillPlugIn
    {
        #region MyRegion

        /// <summary>
        /// 运输路线
        /// </summary>
        readonly ElementKeySchema F_RDLV_ShipRute = new ElementKeySchema
        {
            FormKey = "F_RDLV_ShipRute",
            DbKey = "F_RDLV_SHIPRUTE",
            OrmKey = "F_RDLV_ShipRute"
        };

        /// <summary>
        /// 运输计划点
        /// </summary>
        readonly ElementKeySchema F_RDLV_ShipCode = new ElementKeySchema
        {
            FormKey = "F_RDLV_ShipCode",
            DbKey = "F_RDLV_ShipCode",
            OrmKey = "F_RDLV_ShipCode"
        };

        #endregion

        /// <summary>
        /// 字段变化
        /// </summary>
        /// <param name="e"></param>
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            //if (e.Field.Key.ToUpper() == "F_RDLV_TransferRute".ToUpper())
            //{
            //}
        }

        public override void BeforeF7Select(BeforeF7SelectEventArgs e)
        {
            base.BeforeF7Select(e);

            if (e.FieldKey.EqualsIgnoreCase(F_RDLV_ShipCode.FormKey))
            {
                SetPlanCodeFilter(e);
            }
        }

        private void SetPlanCodeFilter(BeforeF7SelectEventArgs e)
        {
            // 给运输计划点查询附加过滤条件
            var shipRute = View.Model.GetValue(F_RDLV_ShipRute.OrmKey) as DynamicObject;
            var startPoint = shipRute?["F_RDLV_start_point"] as DynamicObject;
            var startPointId = Convert.ToInt32(startPoint?["Id"]);
            var sql = $@"select distinct FID
                from RDLV_t_PlanCodeEntry
                where F_RDLV_START_POINT = {startPointId};";
            var planCodeIdList = DBUtils.ExecuteDynamicObject(Context, sql);
            if (planCodeIdList?.Count > 0)
            {
                var planCodeIds = planCodeIdList.Select(i => Convert.ToInt32(i["FID"]));
                var planCodeIdString = string.Join(",", planCodeIds);
                e.ListFilterParameter.Filter = e.ListFilterParameter.Filter
                    .JoinFilterString($" FID in ({planCodeIdString}) ");
            }
        }

        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            //if(e.BarItemKey.ToUpper()=='SAVE')
            //{

            //}
        }
    }
}

